package com.aptemo.webetek.client.widgets.payments;

import java.util.ArrayList;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.GotAsynchCallback;
import com.aptemo.webetek.client.commands.impl.GetPaymentsPagingAction;
import com.aptemo.webetek.client.enums.EnumPaymentType;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.client.widgets.paging.PagingPresenter;
import com.aptemo.webetek.shared.PagingResult;
import com.aptemo.webetek.shared.PaymentBean;
import com.aptemo.webetek.shared.UserBean;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;

public class PaymentsPagingPresenter
		extends PagingPresenter
{

	private HandlerManager				meventbus;
	private WebetekUserServiceAsync		mservice;
	protected boolean					misAdmin	= false;
	private PaymentsDisplay				display;
	private int							pageNo		= 0;
	protected ArrayList<PaymentBean>	list;

	private EnumPaymentType				type		= EnumPaymentType.ALL;
	private String						teamId;
	private UserBean					currentUser;

	public PaymentsPagingPresenter(UserBean currentUser, PaymentsDisplay view, HandlerManager eventbus, WebetekUserServiceAsync userService, String id,
			String filter)
	{
		this(currentUser, view, eventbus, userService, id, filter, 0);
	}

	public PaymentsPagingPresenter(UserBean currentUser, PaymentsDisplay view, HandlerManager eventbus, WebetekUserServiceAsync userService, String id,
			String filter, int pageNo)
	{
		super(view.getPagingView(), 10);

		this.meventbus = eventbus;
		this.mservice = userService;
		this.display = view;
		this.pageNo = pageNo;
		this.teamId = id;

		init(currentUser, teamId, filter, pageNo);
	}

	private void bind()
	{
		// bind paging
		// display.getPagingView().setVisible(!hidePaging);

		display.setStatusFilterSelected(type);

		display.getList().addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				int row = display.getClickedRow(event);
				PaymentBean stake = list.get(row);
			}
		});

		display.getStatusFilter().addChangeHandler(new ChangeHandler()
		{

			@Override
			public void onChange(ChangeEvent event)
			{
				type = display.getStatusFilterValue();
				resetPaging();
				retrieveList(0);
			}
		});
	}

	private void retrieveList(final int page)
	{
		final String destPage = getCursor(page);

		display.getInfoField().setText(display.translate("loading"));

		GetPaymentsPagingAction action = new GetPaymentsPagingAction(teamId, display.getStatusFilterValue(), destPage, getPageSize());
		mservice.invoke(action, new GotAsynchCallback<PagingResult<PaymentBean>>()
		{

			@Override
			public void onSuccess(PagingResult<PaymentBean> result)
			{
				// paging staff...
				if (result.getTotalCnt() >= 0)
				{
					setPagingTotalSize(result.getTotalCnt());
				}
				pageNo = updatePaging(page, result.getCursor());

				PaymentsPagingPresenter.this.list = result.getList();
				display.getInfoField().setText("");
				display.updatePaymentList(list);
			}

			@Override
			public void onError(String message, EErrorCodes code)
			{
				display.getInfoField().setText(message);
			}

		});
	}

	@Override
	public void go(HasWidgets container)
	{
		container.clear();
		container.add(display.getDisplay());
		bind();

		retrieveList(pageNo);
	}

	private void init(UserBean currentUser, String teamid, String filter, int pageNo)
	{
		this.teamId = teamid;
		this.pageNo = pageNo;
		this.currentUser = currentUser;

		EnumPaymentType status;
		try
		{
			status = EnumPaymentType.valueOf(filter);
		}
		catch (Exception e)
		{
			status = EnumPaymentType.ALL;
		}
		display.setStatusFilterSelected(status);

	}

	public void update(UserBean currentUser, String teamId, String filter, int pageNo)
	{
		init(currentUser, teamId, filter, pageNo);

		retrieveList(pageNo);
	}

}
